package main

import (
	"fmt"
	"log"

	"github.com/tarm/serial"

	"gitee.com/shipnet/serialer"
)

func main() {
	options := serial.Config{
		Name:     "/dev/ttyS1",
		Baud:     9600,
		Size:     8,
		StopBits: 1,
		// MinimumReadSize: 1,
	}

	port, err := serial.OpenPort(&options)
	if err != nil {
		log.Fatal(err)
	}
	defer port.Close()

	// r := bufio.NewReader(port)
	// go func() {
	// 	for {
	// 		bs, err := r.ReadBytes('\n')
	// 		if err != nil {
	// 			log.Printf("read error = %v\n", err)
	// 		}
	// 		fmt.Printf("read = %s\n", string(bs))
	// 	}
	// }()

	s := "PC,S,R,X,X"
	ch := serialer.XorChecksumString([]byte(s))
	s = fmt.Sprintf("$%s*%s\r\n", s, ch)
	log.Printf("write = %s\n", s)
	b := []byte(s)
	wr, err := port.Write(b)
	if err != nil {
		log.Printf("failed wr = %d, err = %v\n", wr, err)
	} else {
		log.Printf("succeed wr = %d\n", wr)
	}
}
